package com.situ.day20.db;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class DBDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //MySql mySql = new MySql();
        //mySql.getConnection();
        //
        //IDB db = new MySql();
        //db.getConnection();
        //db = new SqlServer();
        //db.getConnection();

        IDB db1 = DBFactory.getDB(1);
        db1.getConnection();
        db1 = DBFactory.getDB(2);
        db1.getConnection();

        IDB db2 = DBFactory.getDB();

        //1.通过当前的类获取类加载器
        ClassLoader classLoader = DBDemo.class.getClassLoader();
        //2.通过类加载器的方法获取输入流
        InputStream inputStream = classLoader.getResourceAsStream("db1.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        String className = properties.getProperty("className");
        //com.situ.day20.db.MySql
        Class clazz = Class.forName(className);
        Constructor constructor = clazz.getConstructor();
        IDB db = (IDB) constructor.newInstance();
        db.getConnection();

    }
}
